package com.renderplane;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;

public class OpenGLRenderer2 implements Renderer {
	private final Mesh root;
	private float angle = 0;
	private int angleStep = 5;	// rotation speed in degrees
	
	private static float azimuth = 0;
	private static float pitch = 0;
	private static float roll = 0;
	private static boolean dpadControlled = false;
	private static long time = System.currentTimeMillis();

	public OpenGLRenderer2() {
		// Initialize our cube.
		//Group group = new Group();
		Cube cube = new Cube(1, 1, 1);
		//group.add(cube);
		//root = group;
		root = cube;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}

	public void onDrawFrame(GL10 gl) {
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Replace the current matrix with the identity matrix
		gl.glLoadIdentity();
		// Translates 4 units into the screen.
		gl.glTranslatef(0, 0, -6);
		
		if (dpadControlled) {
			gl.glRotatef(pitch, 1, 0, 0);
			gl.glRotatef(azimuth, 0, 1, 0);
			gl.glRotatef(roll, 0, 0, 1);
		}
		
		else{
			// rotate the square angleStep degrees per frame
			angle = angle%1080;
			if (angle < 360){
				gl.glRotatef(angle, 1, 0, 0);
			}
			else if (angle < 720){
				gl.glRotatef(angle, 0, 1, 0);
			}	
			else {
				gl.glRotatef(angle, 0, 0, 1);			
			}
			angle += angleStep;
		}
		
		// Draw our square.
		root.draw(gl); 		
		
		long t = System.currentTimeMillis() - time;
		time = System.currentTimeMillis();
		Log.d("Framerate", String.valueOf(1000.0f/t));

	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);
		// Reset the projection matrix
		gl.glLoadIdentity();
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();
	}

	public void centerKey() {
		
		if (dpadControlled) {
			dpadControlled = false;
			Log.d("Key", "Dpad control off");
		}
		else {
			dpadControlled = true;
			Log.d("Key", "Dpad control on");
		}
		
	}

	public void modifyPitch(float i) {
		if(dpadControlled){
			Log.d("Key", "pitch modified by "+String.valueOf(angleStep*i));	
			pitch += angleStep * i;
		}
	}

	public void modifyAzimuth(float i) {
		if(dpadControlled){
			Log.d("Key", "azimuth modified by "+String.valueOf(angleStep*i));
			azimuth += angleStep * i;
		}
	}	

	public void modifyRoll(float i) {
		if(dpadControlled){
			Log.d("Key", "roll modified by "+String.valueOf(angleStep*i));
			roll += angleStep * i;
		}
	}
	
	
}
